import { request, requestV2, getCookie } from '@/utils'

const apiServer = ''
const apiRoot = '/'
const API_PATHS = {
  // 判断考勤人员是否在其他考勤组
  CHECK_EMPLOYEE: 'atdc/group/checkEmployee',
  // 创建考勤组
  CREATE_GROUP: 'atdc/group/createGroup',
  // 编辑考勤组
  UPDATE_GROUP: 'atdc/group/updateGroup',
  // 删除考勤组
  DELETE_GROUP: 'atdc/group/deleteGroup',
  // 查询员工当天考勤信息
  QUERY_EMPLOY_RECORD: 'atdc/attend/queryEmployRecord',
  // 查询员工所在的考勤组
  QUERY_OWN_GROUP: 'atdc/group/queryOwnGroup',
  // 管理员查询企业考勤组列表
  QUERY_GROUP_LIST: 'atdc/group/queryGroupList',
  // 管理员考勤组详情查询
  QUERY_GROUP_DETAIL: 'atdc/group/queryGroupDetail',
  // 员工考勤打卡
  ATTENDACE_CHECK: 'atdc/attend/clock',
  // 查询员工月报明细
  QUERY_PERSONAL_MONTHLYREPORT: 'atdc/report/queryPersonalMonthlyReport',
  // 团队日报详情信息
  QUERY_TEAM_DAILYINFO: 'atdc/report/queryTeamDailyInfo',
  // 查询团队日报统计
  QUERY_TEAM_DAILYREPORT: 'atdc/report/queryTeamDailyReport',
  // 查询团队月报统计
  QUERY_TEAM_MONTHLYREPORT: 'atdc/report/queryTeamMonthlyReport',
  // 导出员工月报明细
  SEND_PERSONAL_MONTHLYREPORT: 'atdc/report/sendPersonalMonthlyReport',
  // 导出团队日报明细
  SEND_TEAM_DAILYREPORT: 'atdc/report/sendTeamDailyInfo',
  // 导出团队月报明细
  SEND_TEAM_MONTHLYREPORT: 'atdc/report/sendTeamMonthlyReport',
  // 新建考勤申述流程
  ADD_APPEAL_FLOW: 'atdc/appeal/abnormalAppeal',
  // 查看异常申诉详情
  GET_APPEAL_FLOW: 'atdc/appeal/querySingleAppealInfo',
  // 获取我发起的异常申诉列表
  GET_APPEAL_FLOW_LIST: 'atdc/appeal/queryUserAppealList',
  // 获取待我审批的异常申诉列表
  GET_PENDING_APPEAL_FLOW_LIST: 'atdc/appeal/queryManageAppealList',
  // 管理异常审批
  DEAL_APPEAL: 'atdc/appeal/dealAppeal',
  // 发起人撤销审批
  CANCEL_APPEAL: 'atdc/appeal/cancelAppeal',
  // 查询团队日报分项数据统计
  QUERY_TEAM_DAILY_ITEM: 'atdc/report/queryTeamDailyItem',
  // 查询个人月列表考勤数据统计
  QUERY_EMP_MONTH_LIST: 'atdc/report/queryEmpMonthList'
}

// 拼接请求url
const getRequstPath = (path, server = apiServer, root = apiRoot) => {
  const sid = getCookie('token')
  const rnd = Math.random()

  return `${server}${root}${path}?sid=${sid}&rnd=${rnd}`
}

/**
 * [增加打卡数据]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const sendCheckRecord = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.ATTENDACE_CHECK),
    data: data,
    callback
  })
}

/**
 * [查询我所在组的考勤信息]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const queryMyOwnGroup = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.QUERY_OWN_GROUP),
    data: data,
    callback
  })
}

/**
 * [获取所有考勤组信息]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const queryGroupList = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.QUERY_GROUP_LIST),
    data: data,
    callback
  })
}

/**
 * [获取员工当天的打卡记录]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const getMyRecordsToday = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.QUERY_EMPLOY_RECORD),
    data: data,
    callback
  })
}

/**
 * [新建考勤组]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const addAttendanceGroup = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.CREATE_GROUP),
    data: data,
    callback
  })
}

/**
 * [修改考勤组]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const updateAttendanceGroup = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.UPDATE_GROUP),
    data: data,
    callback
  })
}

/**
 * [删除考勤组]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const deleteAttendanceGroup = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.DELETE_GROUP),
    data: data,
    callback
  })
}

/**
 * [获取考勤组详情]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const getAttendanceGruopDetail = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.QUERY_GROUP_DETAIL),
    data: data,
    callback
  })
}

/**
 * [判断考勤人员是否在其他考勤组]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const checkEmployee = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.CHECK_EMPLOYEE) + (data.attendanceId ? '&attendanceId=' + data.attendanceId : ''),
    data: {employees: data.employees},
    callback
  })
}

/**
 * [导出个人月报详情]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const exportPersonalMonthyReport = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.SEND_PERSONAL_MONTHLYREPORT),
    data: data,
    callback
  })
}

/**
 * [导出团队日报详情]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const exportTeamDailyReport = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.SEND_TEAM_DAILYREPORT),
    data: data,
    callback
  })
}

/**
 * [导出团队月报详情]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const exportTeamMonthyReport = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.SEND_TEAM_MONTHLYREPORT),
    data: data,
    callback
  })
}

// http://IP:PORT/atdc/**/queryTeamDailyInfo&sid
/**
 * 团队日报详情信息
 * 查询员工当天考勤信息
 * @param  {Object}    data          请求参数
 * @param  {String}    data.enterId  企业 ID
 * @param  {String}    data.date     查询内容的日期
 * @param  {Function}  callback      回调
 * @return void
 */
export const queryTeamDailyInfo = (data, callback) => {
  request({
    // url: '/atdc/report/queryTeamDailyInfo?sid=' + getCookie('token'),
    url: getRequstPath(API_PATHS.QUERY_TEAM_DAILYINFO),
    data: data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    method: 'POST',
    success (json) {
      callback && callback(json)
    },
    fail (err) {
      callback && callback(err)
    }
  })
}
/**
 * 查询员工月报明细
 * 供员工本人或者管理员查看个人的考勤月报明细
 * @param  {Object}    data             请求参数
 * @param  {String}    data.enterId     企业 ID
 * @param  {String}    data.uid         企业联系人 ID
 * @param  {String}    data.queryMonth  查询月份
 * @param  {Function}  callback         回调
 * @return void
 */
export const queryPersonalMonthlyReport = (data, callback) => {
  request({
    // url: '/atdc/report/queryPersonalMonthlyReport?sid=' + getCookie('token'),
    url: getRequstPath(API_PATHS.QUERY_PERSONAL_MONTHLYREPORT),
    data: data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    method: 'POST',
    success (json) {
      callback && callback(json)
    },
    fail (err) {
      callback && callback(err)
    }
  })
}
/**
 * 查询团队日报统计
 * 供管理员查看团队的日报考勤统计信息
 * @param  {Object}    data   请求参数
 * @param  {String}    data.enterId  企业 ID
 * @param  {String}    data.queryDay  查询日期
 * @param  {Function}  callback  回调
 * @return void
 */
export const queryTeamDailyReport = (data, callback) => {
  request({
    // url: '/atdc/report/queryTeamDailyReport?sid=' + getCookie('token'),
    url: getRequstPath(API_PATHS.QUERY_TEAM_DAILYREPORT),
    data: data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    method: 'POST',
    success (json) {
      callback && callback(json)
    },
    fail (err) {
      callback && callback(err)
    }
  })
}
/**
 * 查询团队月报统计
 * 供管理员查看团队的月报统计
 * @param  {Object}    data  请求参数
 * @param  {String}    data.enterId  企业 ID
 * @param  {String}    data.queryMonth  查询月份
 * @param  {Function}  callback  回调
 * @retun
 */
export const queryTeamMonthlyReport = (data, callback) => {
  request({
    // url: '/atdc/report/queryTeamMonthlyReport?sid=' + getCookie('token'),
    url: getRequstPath(API_PATHS.QUERY_TEAM_MONTHLYREPORT),
    data: data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    method: 'POST',
    success (json) {
      callback && callback(json)
    },
    fail (err) {
      callback && callback(err)
    }
  })
}
/**
 * 查询员工所在的考勤组
 * 查询员工所在的考勤组，返回员工、考勤组的相关信息
 * @return void
 */
export const queryOwnGroup = (data, callback) => {
  request({
    // url: '/atdc/group/queryOwnGroup?sid=' + getCookie('token'),
    url: getRequstPath(API_PATHS.QUERY_OWN_GROUP),
    data: data,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    method: 'POST',
    success (json) {
      callback && callback(json)
    },
    fail (err) {
      callback && callback(err)
    }
  })
}

/**
 * 新建考勤异常申诉流程
 * @return void
 */
export const addAppealFlow = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.ADD_APPEAL_FLOW),
    data: data,
    callback
  })
}

/**
 * [获取异常审批详情]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const getAppealFlow = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.GET_APPEAL_FLOW),
    data: data,
    callback
  })
}

/**
 * [获取我发起的异常申诉列表]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const getAppealFlowList = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.GET_APPEAL_FLOW_LIST),
    data: data,
    callback
  })
}

/**
 * [获取待我审批的异常申诉列表]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const getPendingAppealFlowList = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.GET_PENDING_APPEAL_FLOW_LIST),
    data: data,
    callback
  })
}

/**
 * [审批异常申述]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const dealAppeal = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.DEAL_APPEAL),
    data: data,
    callback
  })
}

/**
 * [撤销异常申述]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const cancelAppeal = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.CANCEL_APPEAL),
    data: data,
    callback
  })
}

/**
 * [获取群组每日打卡分项明细(迟到、早退、未打卡)]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const queryGroupDailyReportDetail = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.QUERY_TEAM_DAILY_ITEM),
    data: data,
    callback
  })
}

/**
 * [获取个人当月打卡明细]
 * @param  {[type]}   data     [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
export const queryPersonalDailyReportDetail = (data, callback) => {
  requestV2({
    url: getRequstPath(API_PATHS.QUERY_EMP_MONTH_LIST),
    data: data,
    callback
  })
}
